package com.njuse.seecjvm.instructions.references;

import com.njuse.seecjvm.instructions.base.Index16Instruction;
import com.njuse.seecjvm.memory.JHeap;
import com.njuse.seecjvm.memory.jclass.InitState;
import com.njuse.seecjvm.memory.jclass.JClass;
import com.njuse.seecjvm.memory.jclass.runtimeConstantPool.RuntimeConstantPool;
import com.njuse.seecjvm.memory.jclass.runtimeConstantPool.constant.ref.ClassRef;
import com.njuse.seecjvm.runtime.OperandStack;
import com.njuse.seecjvm.runtime.StackFrame;
import com.njuse.seecjvm.runtime.struct.JObject;
import com.njuse.seecjvm.runtime.struct.NonArrayObject;
import com.njuse.seecjvm.classloader.ClassLoader;


public class NEW extends Index16Instruction {
    /**
     * TODO 实现这条指令
     * 其中 对应的index已经读取好了
     */
    @Override
    public void execute(StackFrame frame) {
    	
    	RuntimeConstantPool runtimeConstantPool = frame.getMethod().getClazz().getRuntimeConstantPool();
    	ClassRef t=(ClassRef) runtimeConstantPool.getConstant(index);
     	try {
    	JClass clazz=t.getResolvedClass();
    	if (clazz.getInitState()==InitState.PREPARED) {
    		frame.setNextPC(frame.getNextPC()-3);//倒退回去，如果没有初始化完成，那就倒退回去再做一遍
    		clazz.initClass(frame.getThread(), clazz);
    		return;
    	}
    	NonArrayObject temp=new NonArrayObject(clazz);
    	JHeap.getInstance().addObj(temp);
    	frame.getOperandStack().pushObjectRef(temp);
    	}catch(Exception e) {
    		
    	}
    	


       
    }
}
